from django.db import models
# from django.db.models.base import ModelBase


class BaseModel(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True
        managed = True


class Author(BaseModel):
    name = models.CharField(max_length=255)
    bio = models.TextField()

    class Meta(BaseModel.Meta):
        db_table = "orm_author"
        verbose_name = "作者"
        verbose_name_plural = "作者"
        ordering = ["-updated_at"]

    # def __str__(self):
    #     return f"<Author: {self.name}>"

class Book(BaseModel):
    name = models.CharField(max_length=255)
    author = models.ForeignKey(Author, related_name="book", on_delete=models.CASCADE, db_constraint=False)
    description = models.TextField()
    published_date = models.DateField()

    class Meta(BaseModel.Meta):
        db_table = "orm_book"
        verbose_name = "书籍"
        verbose_name_plural = "书籍"
        ordering = ["-published_date"]

    # def __str__(self):
    #     return f"<Book: {self.name}>"

class Tags(BaseModel):
    name = models.CharField(max_length=255, verbose_name="标签名称", null=False, blank=False, unique=True, db_index=False)
    book = models.ManyToManyField(Book, related_name="tags")

    # def __str__(self):
    #     return f"<Tags: {self.name}>"
    class Meta(BaseModel.Meta):
        db_table = "orm_tags"
        verbose_name = "标签"
        verbose_name_plural = "标签"
        ordering = ["-created_at"]


class Student(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    name = models.CharField(max_length=100, verbose_name="student name", null=False)
    gender = models.SmallIntegerField(choices=((0, '女'), (1, '男')), null=False, blank=False)
    age = models.IntegerField()

    class Meta:
        db_table = "orm_student"
        verbose_name = "学生"
        verbose_name_plural = verbose_name


class Clazz(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    name = models.CharField(max_length=20, verbose_name="Class Name", null=False, blank=False)
    student = models.ForeignKey(to=Student, on_delete=models.DO_NOTHING, db_constraint=False, related_name="clazz")
    class Meta:
        db_table = "orm_class"
        verbose_name = "班级"
        verbose_name_plural = verbose_name

class Teacher(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    name =  models.CharField(max_length=20, verbose_name="Teacher Name", null=False, blank=False)
    student = models.ManyToManyField(to=Student, related_name="teacher", db_constraint=False)
    clazz = models.ManyToManyField(Clazz, related_name="teacher", db_constraint=False)

    class Meta:
        db_table = "orm_teacher"
        verbose_name = "老师"
        verbose_name_plural = verbose_name


class Orm(models.Model):
    name = models.CharField(max_length=10, verbose_name="Orm Name")
